package com.dayaokeji.rhythmschoolstudent.client.home.meeting;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.client.home.meeting.adapter.PersonnelManagementAdapter;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.MeetingApi;
import com.dayaokeji.server_api.domain.Member;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 * 人员管理
 */
public class PersonnelManagementActivity extends BaseActivity {

    public static final String EXTRA_MEETING_ID = "meeting_id";

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_personnel_management_list)
    RecyclerView rvPersonnelManagementList;

    private static final MeetingApi meetingApi = ApiUtils.getApi(MeetingApi.class);
    private Call<ServerResponse<List<Member>>> memberCall;
    private int meetingId;
    private PersonnelManagementAdapter personnelManagementAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personnel_management);
        setSupportActionBar(toolbar);

        meetingId = getIntent().getIntExtra(EXTRA_MEETING_ID,-1);

        if (meetingId == -1) {
            ToastUtils.warning("未获取到会议的ID");
            return;
        }

        init();
    }

    private void init() {
        setRecyclerView();
        getMeetingMemberData();
    }


    private void setRecyclerView() {
        rvPersonnelManagementList.setLayoutManager(new LinearLayoutManager(this));
        rvPersonnelManagementList.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .marginResId(R.dimen.default_divider_height)
                .build());
        personnelManagementAdapter = new PersonnelManagementAdapter();
        rvPersonnelManagementList.setAdapter(personnelManagementAdapter);
        personnelManagementAdapter.setEmptyView(R.layout.empty_layout,rvPersonnelManagementList);

    }

    private void getMeetingMemberData() {
        memberCall = meetingApi.member(meetingId);
        memberCall.enqueue(new ServerCallback<List<Member>>(this,"加载中...") {
            @Override
            public void onResponse(boolean success, ServerResponse<List<Member>> response) {
                if (success) {
                    personnelManagementAdapter.setNewData(response.getBody());
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (memberCall != null) {
            memberCall.cancel();
        }
        super.onDestroy();
    }
}
